package com.sky.controller.admin;

import com.sky.annotation.RefreshRedis;
import com.sky.constant.RedisConstant;
import com.sky.constant.RedisMethodType;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "后台菜品管理")
@Slf4j
public class DishController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishService dishService;
    //新增菜品
    @PostMapping
    @ApiOperation(value = "新增菜品")
    @RefreshRedis(key = RedisConstant.DISH_KEY, method_type = RedisMethodType.DELETE)
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品");
        boolean flag = dishService.saveWithFlavour(dishDTO);

        return flag? Result.success("新增菜品成功") : Result.error("新增菜品失败");
    }

    //获取菜品分页查询
    @GetMapping("/page")
    @ApiOperation(value = "获取菜品列表")
    public Result<PageResult> list(DishPageQueryDTO dishPageQueryDTO) {
        log.info("获取菜品列表");
        PageResult PageResult = dishService.list(dishPageQueryDTO);
        return Result.success(PageResult);
    }

    //根据id获取菜品详情
    @GetMapping("/{id}")
    @ApiOperation(value = "获取菜品详情")
    public Result<DishVO> get(@PathVariable Long id) {
        log.info("获取菜品详情");
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    //菜品起售/停售（单个/批量）
    @PostMapping("/sale")
    @ApiOperation(value = "菜品起售/停售")
    @RefreshRedis(key = RedisConstant.DISH_KEY, method_type = RedisMethodType.DELETE)
    public Result sale(@RequestBody List<Long> ids, @RequestParam(required = false) Boolean saleStatus) {
        //saleStatus为要设置的状态，为空时，默认是停售,false是停售，true是起售
        log.info("菜品起售/停售");
        dishService.sale(ids,saleStatus);
        return Result.success("菜品起售/停售成功");

    }

    //菜品起售
    @PostMapping("/status/{status}")
    @ApiOperation(value = "菜品起售")
    @RefreshRedis(key = RedisConstant.DISH_KEY, method_type = RedisMethodType.DELETE)
    public Result updateStatus(@PathVariable int status,@RequestParam Long id) {
        log.info("菜品起售");
        dishService.updateStatus(id,status);
        return Result.success("菜品起售成功");
    }

    //菜品修改
    @PutMapping
    @ApiOperation(value = "菜品修改")
    @RefreshRedis(key = RedisConstant.DISH_KEY, method_type = RedisMethodType.DELETE)
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("菜品修改");
        //出错直接抛出异常，由全局异常处理器处理
        dishService.update(dishDTO);
        return Result.success("菜品修改成功");
    }

    //批量删除菜品/删除单个菜品
    @DeleteMapping
    @ApiOperation(value = "删除菜品")
    @RefreshRedis(key = RedisConstant.DISH_KEY, method_type = RedisMethodType.DELETE)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品");
        boolean flag = dishService.delete(ids);
        return flag? Result.success("删除菜品成功") : Result.error("删除菜品失败");
    }

    //根据分类id获取菜品列表
    @GetMapping("/list")
    @ApiOperation(value = "根据分类id获取菜品列表")
    public Result<List<DishVO>> listByCategoryId(@RequestParam Long categoryId) {
        log.info("根据分类id获取菜品列表");
//        redisTemplate
        List<DishVO> dishVOList = dishService.listByCategoryId(categoryId);
        return Result.success(dishVOList);
    }
}
